<?php
  require '../../include/library.inc.php';
  require '../../include/admin_login.inc.php';
  require '../../include/paging.inc.php';
  
  $category_id = intval($_GET['cat']);
  $lang = 1;
  $sql = "SELECT
			  c.catalog_id,
			  c.order_number,
			  c.published,
			  
			  d.title
			  
			  
		  FROM
			  catalog AS c,
			  catalog_description AS d
		  WHERE
		  
			  c.catalog_id = d.catalog_id
			  
			  AND
			  
			  d.lang_id = '".$lang."'
			";
			
  if ($category_id) {
	  $sql =	"SELECT
			  c.catalog_id,
			  c.product_th_img,
			  
			  c.order_number,
			  c.published,
			  
			  d.title	
		  FROM
			  catalog AS c,
			  catalog_description AS d,
			  catalog_to_category AS r
		  WHERE
		  
			  c.catalog_id = d.catalog_id
			  AND
			  d.lang_id = '".$lang."'
			  AND
			  c.catalog_id = r.catalog_id
			  AND
			  category_id = '".$category_id."'
			";
	  
  }
  
  
  if (!isset($_GET['sort'])) $_GET['sort'] = 1;
  if (!isset($_GET['sortBy'])) $_GET['sortBy'] = 2;
  
  $paging = new Paging($sql, $_GET['sortBy'], $_GET['sort'], array(2=>"Подредба"), 30);
  $result = $paging->query();
	  
	  head_admin("Продукти");
  ?>
<?php if ($_SESSION['message']) { ?>

<div class="message green">
  <?=$_SESSION['message']?>
</div>
<?php unset($_SESSION['message']); } ?>
<div class="topbuttons"> <a href="catalog_new.php">Добави нов продукт</a> </div>
<h3>Продукти (
  <?=$paging->count()?>
  )</h3>
<table id="listing" width="100%" cellpadding="0" cellspacing="0">
  <thead>
    <tr>
      <td></td>
      <td>Име</td>
      <td>Публикуван</td>
      <td>Подредба</td>
      <td>Действия</td>
    </tr>
  </thead>
  <tbody>
    <?php
				
				
				$i=1;
				while ($row = mysql_fetch_object($result)) {				  
			  ?>
    <tr class="<?=(($i%2)?"none":"zebra")?>">
      <td style="width:200px;">
                    <?php $sql_img = "SELECT image FROM
						catalog_image WHERE catalog_id = '".$row->catalog_id."' ORDER BY top DESC LIMIT 1 "; 
						
						$resimg = query($sql_img);
						$rimg = mysql_fetch_object($resimg);
						$dr = "../catalogimage/".$row->catalog_id."/";
						?>
                    <a href="catalog_edit.php?catalog_id=<?=$row->catalog_id?>"  rel="logos"><img class="thumb" src="<?=$dr?>th_<?=$rimg->image?>" height="100"/></a></td>
      <td><a href="catalog_edit.php?catalog_id=<?=$row->catalog_id?>" title="Редактирай">
        <?=sth($row->title)?>
        </a></td>
      <td class="switch"><?php if ($row->published) { ?>
        <a href="catalog_publish.php?catalog_id=<?=$row->catalog_id?>&publish=0" class="turn_on" name="turn_on" title="Спри"></a>
        <?php } else { ?>
        <a href="catalog_publish.php?catalog_id=<?=$row->catalog_id?>&publish=1" class="turn_off" name="turn_off" title="Публикувай"></a>
        <?php } ?></td>
      <td class="sort"><?php if ($i != 1) { ?>
        <a name="Up" class="sorting_up" title="Нагоре" href="catalog_direction.php?catalog_id=<?=$row->catalog_id?>&direction=1"></a>
        <?php } ?>
        <?php if ($i != ($paging->count())) { ?>
        <a name="Down" class="sorting_down" title="Надолу" href="catalog_direction.php?catalog_id=<?=$row->catalog_id?>&direction=-1"></a>
        <?php } ?>
        &nbsp;<!--ie6 bug--></td>
      <td class="icons"><a name="Top" class="sorting_top" title="Top" href="catalog_direction.php?catalog_id=<?=$row->catalog_id?>&top=1">Top</a> <a href="catalog_edit.php?catalog_id=<?=$row->catalog_id?>" class="edit" title="Редактирай">Редактирай</a> <a onClick="if (window.confirm('Сигурен ли си, че искаш да изтриеш <?=sth($row->title)?>?')) { document.location='catalog_del.php?catalog_id=<?=$row->catalog_id?>'; return false; }" class="delete" title="Изтрий">Изтрий</a></td>
    </tr>
    <?php
				$i++;    
			  } 
		   ?>
  </tbody>
</table>
<div class="pagingwrap">
  <div class="paging">
    <?=$paging->display()?>
  </div>
</div>
<?php
	  foot_admin(); 
  ?>
